﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using FileSynchronizer.DataAccess;
using FileSynchronizer.Objects;


namespace FileSynchronizer.Forms
{
    public partial class Form_Progress : Form
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private FolderPair myFolderPair;
        private const int SourceChanged = 1;
        private const int DestinationChanged = 2;
        private Cursor OldCursor;
        private bool mySyncSuccessfull = false;
        private bool mySyncVisibleSetting;
        private SyncProcessor mySyncProcessor;
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public variables and properties
        //---------------------------------------------------------------------------------------------------------------------------------------
        public bool SyncSuccessfull
        {
            get
            {
                return mySyncSuccessfull;
            }
        }

        public bool SyncVisible
        {
            get
            {
                return mySyncVisibleSetting;
            }
            set
            {
                mySyncVisibleSetting = value;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------       
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------

        public Form_Progress()
        {
            InitializeComponent();

            myFolderPair = null;
            button_Done.Enabled = false;
            mySyncVisibleSetting = true;
        }

        public Form_Progress(FolderPair TheFolderPair)
        {
            InitializeComponent();

            myFolderPair = TheFolderPair;
            button_Done.Enabled = false;
            mySyncVisibleSetting = true;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Event Handlers
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the Form is first shown
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void Form_Progress_Shown(object sender, EventArgs e)
        {
            this.Synchronize();
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the User clicks the "Done" button
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void button_Done_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Synchronization of a FolderPair
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Initiation of the Synchronization
        //---------------------------------------------------------------------------------------------------------------------------------------
        public void Synchronize()
        {
            if (myFolderPair != null)
            {
                OldCursor = this.Cursor;
                this.Cursor = Cursors.WaitCursor;

                textBox1.Text = myFolderPair.LeftFolder;
                textBox2.Text = myFolderPair.RightFolder;

                mySyncProcessor = new SyncProcessor();
                mySyncProcessor.SyncFolderPair(myFolderPair, SyncFolderPairProgressChanged, SyncFolderPairCompleted);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Callback Function which is called whenever the synchronization of a folderpair is complete
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            mySyncSuccessfull = true;
            button_Done.Enabled = true;
            this.Cursor = OldCursor;
            if (mySyncVisibleSetting == false)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Callback Function which is called whenever the synchronization of a file- or folder-ITEM is complete
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            switch (e.ProgressPercentage)
            {
                case SourceChanged:

                    Label_LeftCreated.Text = mySyncProcessor.SourceSyncStatus.FilesCreatedCount.ToString();
                    Label_LeftDeleted.Text = mySyncProcessor.SourceSyncStatus.FilesDeletedCount.ToString();
                    Label_LeftRenamed.Text = mySyncProcessor.SourceSyncStatus.FilesRenamedCount.ToString();
                    Label_LeftUpdated.Text = mySyncProcessor.SourceSyncStatus.FilesUpdatedCount.ToString();
                    break;

                case DestinationChanged:

                    Label_RightCreated.Text = mySyncProcessor.DestinationSyncStatus.FilesCreatedCount.ToString();
                    Label_RightDeleted.Text = mySyncProcessor.DestinationSyncStatus.FilesDeletedCount.ToString();
                    Label_RightRenamed.Text = mySyncProcessor.DestinationSyncStatus.FilesRenamedCount.ToString();
                    Label_RightUpdated.Text = mySyncProcessor.DestinationSyncStatus.FilesUpdatedCount.ToString();
                    break;
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}
